package com.mgs.admin.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mgs.admin.api.dto.UserDTO;
import com.mgs.admin.api.dto.UserInfo;
import com.mgs.admin.api.entity.User;
import com.mgs.admin.api.vo.UserInfoVO;
import com.mgs.admin.service.UserService;
import com.mgs.common.core.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author zhanglei
 * @Date 2021/12/16 16:49
 * @Version 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取当前用户全部信息
     *
     * @return 用户信息
     */
    @GetMapping("/info/{userName}")
    public Result userInfo(@PathVariable String userName) {
        User user = userService.getOne(Wrappers.<User>query().lambda().eq(User::getUserName, userName));
        if (user == null) {
            return Result.failed("获取当前用户信息失败");
        }
        UserInfo userInfo = userService.getUserInfo(user);
        UserInfoVO vo = new UserInfoVO();
        vo.setUser(userInfo.getUser());
        return Result.ok(vo);
    }

    /**
     * 分页查询用户
     *
     * @param page    参数集
     * @param userDTO 查询参数列表
     * @return 用户集合
     */
    @GetMapping("/page")
    public Result getUserPage(Page page, UserDTO userDTO) {
        return Result.ok(userService.getUserPage(page, userDTO));
    }


}
